package com.hsnn.medstgmini.subscribe.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.hsnn.medstgmini.Constants;
import com.hsnn.medstgmini.subscribe.model.BidSubscribe;
import com.hsnn.medstgmini.subscribe.model.BidSubscribeDetail;
import com.hsnn.medstgmini.subscribe.model.BidSubscribeRecord;
import com.hsnn.medstgmini.subscribe.model.form.SuccessDate;
import com.hsnn.medstgmini.subscribe.service.BidSubscribeDetailManager;
import com.hsnn.medstgmini.subscribe.service.BidSubscribeManager;
import com.hsnn.medstgmini.subscribe.service.BidSubscribeRecordManager;
import com.hsnn.medstgmini.sys.model.SysUser;
import com.hsnn.medstgmini.util.Pagination;
import com.hsnn.medstgmini.util.SelectForm;
/**
 * @category 企业详情控制器
 * @author 韩守松
 * @date 2015年9月1日
 */
@Controller
@RequestMapping("/bidSubscribeDetail")
public class BidSubscribeDetailController {
	
	private  static final  Logger log = Logger.getLogger(BidSubscribeDetailController.class);	  

	@Autowired
	private BidSubscribeDetailManager bidSubscribeDetailManager; 
	
	@Autowired
	private BidSubscribeRecordManager bidSubscribeRecordManager;
	
	@Autowired
	private BidSubscribeManager bidSubscribeManager;
	
	@Autowired
	private HttpServletRequest request;
	
	/**
	 * @category 保存企业详情数据信息
	 * @author 韩守松
	 * @date   2015年9月1日
	 * @param  @return
	 */
	@RequestMapping("/saveBidSubscribeDetail")
	@ResponseBody
	public Pagination saveBidSubscribeDetail(Pagination page){
		BidSubscribeDetail bidSubscribeDetail = new BidSubscribeDetail();
		try{	
		String bidSubscribeId = request.getParameter("bidSubscribeId");
		String subscribeDate = request.getParameter("subscribeDate");
		String startTime = request.getParameter("subscribeStarttime");
		String endTime = request.getParameter("subscribeEndtime");
		bidSubscribeDetail.setBidSubscribeId(bidSubscribeId);
		bidSubscribeDetail.setSubscribeDate(new SimpleDateFormat("yyyy-MM-dd").parse(subscribeDate));
		bidSubscribeDetail.setSubscribeStarttime(new SimpleDateFormat("HH:mm:ss").parse(startTime));
		bidSubscribeDetail.setSubscribeEndtime(new SimpleDateFormat("HH:mm:ss").parse(endTime));
		SysUser user = (SysUser)request.getSession().getAttribute(Constants.USERINFO);
		String compId = user.getId();
		String bidSubscribeDetailId = bidSubscribeDetailManager.getRecordDetailId(bidSubscribeDetail);
		String bidSubscribeRecordId = bidSubscribeRecordManager.getCompNum(compId,bidSubscribeId,bidSubscribeDetailId);
		if(StringUtils.isNotBlank(bidSubscribeRecordId) ){
			page.setMsg("不可以重复预约同一时间段");
			page.setSuccess(false);
			return page;
		}else{
		BidSubscribeRecord bidSubscribeRecord = new BidSubscribeRecord();
		bidSubscribeRecord = bidSubscribeRecordManager.getHaveRecordId(bidSubscribeDetail, compId); //获取已预约过的记录id
		if(null != bidSubscribeRecord){
			if(null != bidSubscribeRecord.getProcessStatus()){
				if(bidSubscribeRecord.getProcessStatus() == 1){
					page.setMsg("你预约的项目已处理");
					page.setSuccess(false);
					return page;
				}else{
					bidSubscribeDetail.setRecordId(bidSubscribeRecord.getRecordId());
					bidSubscribeDetail.setDetailId(bidSubscribeRecord.getDetailId());
				}
				
			}
			
		}
		bidSubscribeDetail.setUpdUser(user.getId());
		bidSubscribeDetail.setUpdDatetime(new Date());
		synchronized(BidSubscribeDetail.class){
			Integer compNumber = bidSubscribeRecordManager.getCompIdNumber(bidSubscribeDetailId);//已预约企业数量
			BidSubscribe bidSubscribe = new BidSubscribe();
			bidSubscribe.setBidSubscribeId(bidSubscribeDetail.getBidSubscribeId());
			bidSubscribe.setProcessStartTimeThree(bidSubscribeDetail.getSubscribeStarttime());
			bidSubscribe.setProcessEndTimeThree(bidSubscribeDetail.getSubscribeEndtime());
			Integer untilNumber = bidSubscribeManager.getUntilNumber(bidSubscribe);//企业预约上限数量
			if(compNumber < untilNumber){
				bidSubscribeDetailManager.proSubscribeSave(bidSubscribeDetail);
				page.setMsg("预约成功");
				page.setSuccess(true);
			}else{
				page.setMsg("预约人数达到上限");
				page.setSuccess(false);
			}
		  }
		}
		}catch(Exception e){
			log.error("保存企业预约信息失败"+e);
			page.setSuccess(false);
		}
		return page;
	}
	
	/**
	 * @category 跳转到生产企业新增预约页面
	 * @author 韩守松
	 * @date   2015年8月31日
	 * @param  @return
	 */
	@SuppressWarnings("rawtypes")
	@RequestMapping("/toProduceSubscribeAddJsp")
	public String toProduceSubscribeAddJsp(String bidSubscribeId){
		try{
		BidSubscribeDetail bidSubscribeDetail = new BidSubscribeDetail();
		SysUser user = (SysUser)request.getSession().getAttribute(Constants.USERINFO);
		String compId = user.getId();
		request.setAttribute("projName",java.net.URLDecoder.decode( request.getParameter("projName"),"UTF-8"));
		List<BidSubscribeDetail> listDate = new ArrayList<BidSubscribeDetail>();
		List<BidSubscribeDetail> listTime = new ArrayList<BidSubscribeDetail>();
		List list = new ArrayList();
		list = bidSubscribeManager.getBidSubscribeById(bidSubscribeId);
		listDate = bidSubscribeDetailManager.getSubscribeDateList(bidSubscribeId);
		if(listDate.size() > 0){
				bidSubscribeDetail.setBidSubscribeId(bidSubscribeId);
				bidSubscribeDetail.setSubscribeDate(listDate.get(0).getSubscribeDate());
				listTime = bidSubscribeDetailManager.getSubscribeTimeList(bidSubscribeDetail);
		}
		List subOverlist = bidSubscribeRecordManager.getSuccessData(compId,bidSubscribeId);
		if(subOverlist.size() > 0){
			request.setAttribute("subOverlist", subOverlist.get(0));
		}
		if(list.size() > 0){
			request.setAttribute("list", list.get(0));
		}
		request.setAttribute("listDate", listDate);
		request.setAttribute("listTime", listTime);
		request.setAttribute("bidSubscribeId", bidSubscribeId);
		request.setAttribute("defalutProjId", request.getParameter("defalutProjId"));
		}catch(Exception e){
			e.printStackTrace();
		}
		return "/subscribe/produce/newAdd";
	}
	
	/**
	 * @category 获取预约日期下拉列表
	 * @author 韩守松
	 * @date   2015年9月6日
	 * @param  @param page
	 * @param  @param bidSubscribeDetail
	 * @param  @return
	 */
	@RequestMapping("/toNewAddJsp")
	@ResponseBody
	public List<SelectForm> toNewAddJsp(Pagination page,
			BidSubscribeDetail bidSubscribeDetail){
		List<BidSubscribeDetail> list = new ArrayList<BidSubscribeDetail>();
		List<SelectForm> selectFormlist = new ArrayList<SelectForm>();
		list = bidSubscribeDetailManager.getSubscribeDateList(bidSubscribeDetail.getBidSubscribeId());
		if (list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				bidSubscribeDetail = list.get(i);
				SelectForm form = new SelectForm();
				form.setValue(bidSubscribeDetail.getSubscribeDateStr());
				form.setText(bidSubscribeDetail.getSubscribeDateStr());
				selectFormlist.add(form);
			}
		}
		return selectFormlist;
	}
	
	/**
	 * @category 获取时间下来列表
	 * @author 韩守松
	 * @date   2015年9月6日
	 * @param  @param page
	 * @param  @param bidSubscribeDetail
	 * @param  @return
	 */
	@RequestMapping("/toNewAddTimeJsp")
	@ResponseBody
	public List<SelectForm> toNewAddTimeJsp(Pagination page,
			BidSubscribeDetail bidSubscribeDetail){
		List<BidSubscribeDetail> list = new ArrayList<BidSubscribeDetail>();
		List<SelectForm> selectFormlist = new ArrayList<SelectForm>();
		list = bidSubscribeDetailManager.getSubscribeTimeList(bidSubscribeDetail);
		
		if (list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				bidSubscribeDetail = list.get(i);
				SelectForm form = new SelectForm();
				form.setValue(bidSubscribeDetail.getSubscribeStarttimeStr()+" - "+bidSubscribeDetail.getSubscribeEndtimeStr());
				form.setText(bidSubscribeDetail.getSubscribeStarttimeStr()+" - "+bidSubscribeDetail.getSubscribeEndtimeStr());
				selectFormlist.add(form);
			}
		}
		return selectFormlist;
	}
	
	/**
	 * @category 根据日期 变更时间段
	 * @author 韩守松
	 * @date   2015年9月21日
	 * @param  @return
	 */
	@SuppressWarnings("rawtypes")
	@RequestMapping("/changeTime")
	@ResponseBody
	public List<BidSubscribeDetail> changeTime(Pagination page,String subscribeDate,String bidSubscribeId){
		BidSubscribeDetail bidSubscribeDetail = new BidSubscribeDetail();
		List<BidSubscribeDetail> listTime = new ArrayList<BidSubscribeDetail>();
		try{
		SysUser user  = (SysUser)request.getSession().getAttribute(Constants.USERINFO);
		String defaultProjId = user.getDefaultProjId();
		String compId = user.getId();
		if(StringUtils.isNotBlank(defaultProjId)){
			bidSubscribeDetail.setDefaultProjId(defaultProjId);
		}
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Date date = format.parse(subscribeDate);
		List subOverlist = bidSubscribeRecordManager.getSuccessData(compId,bidSubscribeId);
		if(subOverlist.size() > 0){
					String result = new Gson().toJson(subOverlist.get(0));
					
					SuccessDate successDate =  new Gson().fromJson(result, SuccessDate.class);  
					bidSubscribeDetail.setSuccessDate(successDate);
		}
		
		listTime = bidSubscribeDetailManager.getSubscribeTimeByDate(date,bidSubscribeId );
		listTime.add(listTime.size(), bidSubscribeDetail);
		}catch(Exception e){
			log.error("获取预约时间失败");
		}
		return listTime;
	}
}


